﻿using System;

namespace Unis.Framework.RuleBase.Attributes
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)]
    public class ObjectDataMappingAttribute : Attribute
    {
        // Fields
        private bool _CheckReturnValue;
        private bool _DeleteNotInIds;
        private string _Description;
        private Type _EntityType;
        private bool _ExecByRule;
        private bool _ExecBySqlCmd;
        private string _ForeingKeyName;
        private bool _IncludeSubmitField;
        private bool _KeyIsSelfAdd;
        private string _KeyName;
        private string _MappingTableName;
        private string _MappingXmlFileName;
        private bool _ReadOnly;
        private string _TableAlias;
        private string _XmlCfgSelectSqlName;
        private static readonly string DEFAULT_KEY_NAME = "ID";

        // Methods
        public ObjectDataMappingAttribute(string mappingXmlFileName)
            : this(mappingXmlFileName, false, true)
        {
        }

        public ObjectDataMappingAttribute(string mappingXmlFileName, string selectSql)
        {
            this._IncludeSubmitField = true;
            this._ExecBySqlCmd = false;
            this._MappingXmlFileName = mappingXmlFileName;
            this._MappingTableName = mappingXmlFileName;
            this._KeyIsSelfAdd = true;
        }

        public ObjectDataMappingAttribute(string mappingXmlFileName, bool execBySqlCmd, bool keyIsSelfAdd)
        {
            this._IncludeSubmitField = true;
            this._ExecBySqlCmd = execBySqlCmd;
            this._MappingXmlFileName = mappingXmlFileName;
            this._MappingTableName = mappingXmlFileName;
            this._KeyIsSelfAdd = keyIsSelfAdd;
        }

        // Properties
        public bool CheckReturnValue
        {
            get
            {
                return this._CheckReturnValue;
            }
            set
            {
                this._CheckReturnValue = value;
            }
        }

        public bool DeleteNotInIds
        {
            get
            {
                return this._DeleteNotInIds;
            }
            set
            {
                this._DeleteNotInIds = value;
            }
        }

        public string Description
        {
            get
            {
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }

        public Type EntityType
        {
            get
            {
                return this._EntityType;
            }
            set
            {
                this._EntityType = value;
            }
        }

        public bool ExecByRule
        {
            get
            {
                return this._ExecByRule;
            }
            set
            {
                this._ExecByRule = value;
            }
        }

        public bool ExecBySqlCmd
        {
            get
            {
                return this._ExecBySqlCmd;
            }
            set
            {
                this._ExecBySqlCmd = value;
            }
        }

        public string ForeingKeyName
        {
            get
            {
                return this._ForeingKeyName;
            }
            set
            {
                this._ForeingKeyName = value;
            }
        }

        public bool IncludeSubmit
        {
            get
            {
                return this._IncludeSubmitField;
            }
            set
            {
                this._IncludeSubmitField = value;
            }
        }

        public bool KeyIsSelfAdd
        {
            get
            {
                return this._KeyIsSelfAdd;
            }
            set
            {
                this._KeyIsSelfAdd = value;
            }
        }

        public string KeyName
        {
            get
            {
                if (string.IsNullOrEmpty(this._KeyName))
                {
                    return DEFAULT_KEY_NAME;
                }
                return this._KeyName;
            }
            set
            {
                this._KeyName = value;
            }
        }

        public string MappingTableName
        {
            get
            {
                return this._MappingTableName;
            }
            set
            {
                this._MappingTableName = value;
            }
        }

        public string MappingXmlFileName
        {
            get
            {
                return this._MappingXmlFileName;
            }
            set
            {
                this._MappingXmlFileName = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return this._ReadOnly;
            }
            set
            {
                this._ReadOnly = value;
            }
        }

        public string TableAlias
        {
            get
            {
                return this._TableAlias;
            }
            set
            {
                this._TableAlias = value;
            }
        }

        public string XmlCfgSelectSqlName
        {
            get
            {
                return this._XmlCfgSelectSqlName;
            }
            set
            {
                this._XmlCfgSelectSqlName = value;
            }
        }
    }
}
